//
//  ParkCell.m
//  RENTVISITOR
//
//  Created by wonder on 2017/10/24.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "ParkCell.h"
#import "RentVieitor.h"
#import "BuildingModel.h"
@implementation ParkCell{
    UIImageView *imgview;//小区主图
    UILabel *city;//城市
    UILabel *name;//小区名
    UILabel *district;//区县
    UILabel *region;//区/县
    UILabel *address;//地址
    UILabel *describeLabel;//详细信息
    UILabel *label;//标签
    UILabel *d_niandai;//建筑年代
    UILabel *d_chewei;//车位
    UILabel *d_hushu;//户数
    UILabel *d_mianji;//总建筑面积
    UILabel *rent_min;//最少租金
    UILabel *rent_max;//最大租金
    UILabel *amount;//出租房源数量
    UILabel *tagLabel;
    UILabel *tagLabel2;
    UILabel *tagLabel3;
    UILabel *price;
    UILabel *siteLabel;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        //        self.contentView.backgroundColor = [UIColor redColor];
        self.selectionStyle = UITableViewCellEditingStyleNone;
        
        [self creatView];
    }
    return self;
}


- (void)creatView {
    
    __weak typeof(self) weakSelf = self;
    
    imgview = [[UIImageView alloc] init];
    imgview.image = [UIImage imageNamed:@"recomimg_01"];
    [self.contentView addSubview:imgview];
    
    [imgview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(15);
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(15);
        make.size.mas_equalTo(CGSizeMake(110*Main_Scale, 80*Main_Scale));
    }];
    //小区名字
    name = [[UILabel alloc] init];
    name.text = @"芳华香榭";
    name.numberOfLines = 0;
    name.font = [UIFont systemFontOfSize:15*Main_Scale];
    name.textColor = [Utils colorConvertFromString:@"#333333"];
    name.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgview.mas_right).with.offset(10);
        make.top.mas_equalTo(imgview.mas_top).with.offset(0);
        make.height.mas_equalTo(ScaleHeight*30);
    }];
    //房源数量
    amount = [[UILabel alloc] init];
    amount.text = @"公有在租房源18套";
    amount.numberOfLines = 0;
    amount.font = [UIFont systemFontOfSize:13*Main_Scale];
    amount.textColor = [Utils colorConvertFromString:@"#333333"];
    amount.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:amount];
    [amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(name.mas_right).with.offset(ScaleWidth*12);
        make.top.mas_equalTo(imgview.mas_top).with.offset(ScaleHeight*6);
//        make.size.mas_equalTo(CGSizeMake(ScaleWidth*190, ScaleHeight*22));
        make.height.mas_equalTo(ScaleHeight*22);
    }];
//  定位的图表
    UIImageView *location = [[UIImageView alloc] init];
    location.image = [UIImage imageNamed:@"位置"];
    [self.contentView addSubview:location];
    
    [location mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.left.mas_equalTo(imgview.mas_right).with.offset(10*Main_Scale);
//        make.top.mas_equalTo(introduce.mas_bottom).with.offset(10*Main_Scale);
//        make.size.mas_equalTo(CGSizeMake(12*Main_Scale, 12*Main_Scale));
        make.left.mas_equalTo(name.mas_left).mas_offset(0);
        make.top.mas_equalTo(name.mas_bottom).mas_offset(ScaleHeight*16);
        make.size.mas_equalTo(CGSizeMake(ScaleWidth*18, ScaleHeight*22));
    }];
    
    district = [[UILabel alloc] init];
    district.text = @"雅颂居-雅颂居-雅颂居";
    district.font = [UIFont systemFontOfSize:11*Main_Scale];
    district.textColor = [Utils colorConvertFromString:@"#101010"];
    district.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:district];
    
    [district mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(location.mas_right).with.offset(3);
        make.top.mas_equalTo(name.mas_bottom).with.offset(16*ScaleHeight);
//        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-10);
//        make.height.mas_equalTo(12*Main_Scale);
        make.size.mas_equalTo(CGSizeMake(ScaleWidth*232, ScaleHeight*22));
    }];
    
    UIImageView *describe = [[UIImageView alloc] initWithFrame:CGRectMake(135, 56, 12, 12)];
    describe.image = [UIImage imageNamed:@"概况"];
    [self.contentView addSubview:describe];
    
    [describe mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(location.mas_left);
        make.top.mas_equalTo(location.mas_bottom).offset(16*ScaleHeight);
        make.size.mas_equalTo(CGSizeMake(20*ScaleWidth, 20*ScaleHeight));
    }];
    
    
//    describeLabel = [[UILabel alloc] initWithFrame:CGRectMake(155,56,KWidth-155 ,12)];
    //建造时间
    describeLabel = [[UILabel alloc]init];
    describeLabel.text = @"2室一厅一卫|83m²|高层(共22层)";
    describeLabel.numberOfLines = 0;
    describeLabel.font = [UIFont systemFontOfSize:12];
    describeLabel.textColor = [Utils colorConvertFromString:@"#333333"];
    describeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:describeLabel];
    
    [describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(describe.mas_right).with.offset(2);
        make.top.mas_equalTo(describe.mas_top).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(ScaleWidth*378,ScaleHeight*24));
//        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-3);
//        make.height.mas_equalTo(12*Main_Scale);
    }];
    
    tagLabel = [[UILabel alloc] init];
    tagLabel.text = @"学校附近";
    tagLabel.hidden = YES;
    tagLabel.font = [UIFont systemFontOfSize:10];
    tagLabel.textColor = [Utils colorConvertFromString:@"#e2a438"];
    tagLabel.layer.borderWidth = 1;
    tagLabel.textAlignment = NSTextAlignmentCenter;
    tagLabel.layer.masksToBounds = YES;
    tagLabel.layer.cornerRadius = 1.5;
    tagLabel.layer.borderColor = [Utils colorConvertFromString:@"#e2a438"].CGColor;
    [self.contentView addSubview:tagLabel];
    
    
    tagLabel2 = [[UILabel alloc] init];
    tagLabel2.text = @"地铁周边";
    tagLabel2.hidden = YES;
    tagLabel2.font = [UIFont systemFontOfSize:10];
    tagLabel2.textColor = [Utils colorConvertFromString:@"#e8376d"];
    tagLabel2.layer.borderWidth = 1;
    tagLabel2.textAlignment = NSTextAlignmentCenter;
    tagLabel2.layer.masksToBounds = YES;
    tagLabel2.layer.cornerRadius = 1.5;
    tagLabel2.layer.borderColor = [Utils colorConvertFromString:@"#e8376d"].CGColor;
    [self.contentView addSubview:tagLabel2];
    
    tagLabel3 = [[UILabel alloc] init];
    tagLabel3.text = @"购物方便";
    tagLabel3.hidden = YES;
    tagLabel3.font = [UIFont systemFontOfSize:10];
    tagLabel3.textColor = [Utils colorConvertFromString:@"#2dafbe"];
    tagLabel3.layer.borderWidth = 1;
    tagLabel3.textAlignment = NSTextAlignmentCenter;
    tagLabel3.layer.masksToBounds = YES;
    tagLabel3.layer.cornerRadius = 1.5;
    tagLabel3.layer.borderColor = [Utils colorConvertFromString:@"#2dafbe"].CGColor;
    [self.contentView addSubview:tagLabel3];
    CGSize size = [@" 购物方便   " boundingRectWithSize:CGSizeMake(KWidth, 10*Main_Scale) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:tagLabel3.font} context:nil].size;
    [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(imgview.mas_right).with.offset(10*Main_Scale);
        make.top.mas_equalTo(describeLabel.mas_bottom).with.offset(10*Main_Scale);
        make.width.mas_offset(size.width);
        make.height.mas_equalTo(15*Main_Scale);
    }];
    
    [tagLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(tagLabel.mas_right).with.offset(20*Main_Scale);
        make.top.mas_equalTo(describeLabel.mas_bottom).with.offset(10*Main_Scale);
        make.width.mas_offset(size.width);
        make.height.mas_equalTo(15*Main_Scale);
    }];
    
    [tagLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(tagLabel2.mas_right).with.offset(20*Main_Scale);
        make.top.mas_equalTo(describeLabel.mas_bottom).with.offset(10*Main_Scale);
        make.width.mas_offset(size.width);
        make.height.mas_equalTo(15*Main_Scale);
    }];
    
    
    price = [[UILabel alloc] init];
    price.text = @"7800元/月";
    price.font = [UIFont systemFontOfSize:15];
    price.textColor = [Utils colorConvertFromString:@"#e52323"];
    price.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:price];
    
    [price mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(imgview.mas_right).with.offset(10*Main_Scale);
        make.top.mas_equalTo(tagLabel.mas_bottom).with.offset(10*Main_Scale);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-10);
        make.height.mas_equalTo(15*Main_Scale);
    }];
    

    siteLabel = [[UILabel alloc] init];
    siteLabel.text = @"深圳区南山区科技中三路13号";
    siteLabel.font = [UIFont systemFontOfSize:12];
    siteLabel.textColor = [Utils colorConvertFromString:@"#fc7f33"];
    siteLabel.backgroundColor = [Utils colorConvertFromString:@"#fff2ea"];
    siteLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:siteLabel];
    
    [siteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(30);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-30);
        make.top.mas_equalTo(price.mas_bottom).with.offset(8);
        make.height.mas_equalTo(@(30*Main_Scale));
        
    }];
}

- (void)setModel:(BuildingModel *)model  {
    
    _model = model;
    [imgview sd_setImageWithURL:[NSURL URLWithString:_model.image] placeholderImage:[UIImage imageNamed:@"未载入img"]];
    //    [imgview sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    name.text = _model.name;
    NSString *str = [NSString stringWithFormat:@"%@-%@元/月",_model.rent_min,_model.rent_max];
    price.text = [NSString stringWithFormat:@"%@-%@元/月",_model.rent_min,_model.rent_max];
    price.textColor    = RGBCOLOR(229, 35, 35);
    //富文本实现
    NSArray *tempArr = [str componentsSeparatedByString:@"元"];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:str];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:17.0]
                          range:NSMakeRange(0, ((NSString *)tempArr[0]).length)];
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:11.0]
     
                          range:NSMakeRange(((NSString *)tempArr[0]).length, ((NSString *)tempArr[1]).length+1)];
    price.attributedText = AttributedStr;
    NSString *strAmount = [NSString stringWithFormat:@"共有在租房源%@套",_model.amount];
    NSString *length = [NSString stringWithFormat:@"%ld",[strAmount length]];
    NSArray *amountArr = [strAmount componentsSeparatedByString:@"源"];
    NSMutableAttributedString *AmountStr = [[NSMutableAttributedString alloc]initWithString:strAmount];
    [AmountStr addAttribute:NSForegroundColorAttributeName value:RGBA(51, 51, 51, 1)  range:NSMakeRange(0, ((NSString *)amountArr[0]).length+1)];
    [AmountStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(((NSString *)amountArr[0]).length+1, ((NSString *)tempArr[1]).length)];
    [AmountStr addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(51, 51, 51) range:NSMakeRange(strAmount.length - 1,1)];
//    [AmountStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} range:NSMakeRange(0, ((NSString *)amountArr[0]).length+1)];
//    [AmountStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} range:NSMakeRange(((NSString *)amountArr[0]).length+1, ((NSString *)tempArr[1]).length)];
//    [AmountStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} range:NSMakeRange(strAmount.length - 1,1)];
    amount.attributedText = AmountStr;
    if ([_model.d_niandai isEqualToString:@""]) {
        _model.d_niandai = @"0";
    }
    if ([_model.d_hushu isEqualToString:@""]) {
        _model.d_hushu = @"0";
    }
    if ([_model.d_chewei isEqualToString:@""]) {
        _model.d_chewei = @"0";
    }
    
    NSString *str1 = [NSString stringWithFormat:@"%@年建造|%@户|%@停车位",_model.d_niandai,_model.d_hushu,_model.d_chewei];
    
    describeLabel.text = str1;
    NSString *cityStr = [NSString stringWithFormat:@"%@-%@-%@",_model.city,_model.district,_model.region];
    district.text = cityStr;
    NSString *mString = model.label;
    
    if (mString == nil) {
        mString = @"";
    }
    NSString *address = _model.address;
    if (address == nil) {
        address = @"";
    }
    if ([address isEqualToString:@""]) {
        
        [siteLabel removeFromSuperview];
    }
    
    
    NSArray* array;
    if ([mString isEqualToString:@""] == NO) {
        
        mString = [mString stringByReplacingOccurrencesOfString:@"["withString:@""];
        mString = [mString stringByReplacingOccurrencesOfString:@"]"withString:@""];
        mString = [mString stringByReplacingOccurrencesOfString:@"\""withString:@""];
        array = [mString componentsSeparatedByString:@","];
    }
    
    
    if (array.count >=3) {
        
        tagLabel3.hidden = NO;
        tagLabel3.text = array[2];
    }
    
    if (array.count >=2) {
        
        tagLabel2.hidden = NO;
        tagLabel2.text = array[1];
    }
    if (array.count >=1) {
        if ([array[0] isEqualToString:@""]) {
            return;
        }
        tagLabel.hidden = NO;
        tagLabel.text = array[0];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
